﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Business.Stores
{
    public class CashInOutFlowsManager
    {
        private MyCommerceEntities vContext;

        public CashInOutFlowsManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CashInOutFlowsManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public void CashInOutFlowsInsert(CashInOutFlows objItem)
        {
            var objUser = (from em in vContext.User
                               where em.IdUser == objItem.IdEmployee
                               select em).FirstOrDefault();
            if (objUser == null)
                throw new Exception("No se encontró un empleado asociado al usuario que esta ingresando la información.");

            objItem.IdEmployee = objUser.IdEmployee;
            objItem.ProcessDate = DateTime.Today;
            vContext.AddToCashInOutFlows(objItem);
            vContext.SaveChanges();
        }

        public void CashInOutFlowsDelete(long idItemToDelete)
        {
            var objItemFind = (from em in vContext.CashInOutFlows
                               where em.IdCashInOutFlows == idItemToDelete
                               select em).FirstOrDefault();
            if (objItemFind != null)
            {
                vContext.DeleteObject(objItemFind);
                vContext.SaveChanges();
            }
        }

        public List<CashInOutFlows> GetListByDate(DateTime dateToLoad)
        {
            List<CashInOutFlows> colData = new List<CashInOutFlows>();
            var colResult = (from em in vContext.CashInOutFlows
                             .Include("Employee")
                             .Include("Classifier")
                               where em.ProcessDate == dateToLoad
                               select em).AsEnumerable();
            if (colResult != null)
                colData = new List<CashInOutFlows>(colResult);
            return colData;
        }
    }
}
